package com.haieros.www.purerunshowsystem.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.haieros.www.purerunshowsystem.MusicModel;
import com.haieros.www.purerunshowsystem.R;

import java.util.List;

/**
 * Created by admin on 2017/11/22.
 */

public class MyMusicAdapter extends RecyclerView.Adapter<MyMusicAdapter.ViewHolder>{

    private Context mContext;
    private List<MusicModel> mListMusic;
    private Intent mIntent;

    public MyMusicAdapter(Context context, List<MusicModel> listMusic, Intent intent){
        this.mContext = context;
        this.mListMusic = listMusic;
        this.mIntent = intent;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_music,null));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        if (position == 0){
            holder.mView.requestFocus();
            ViewCompat.animate(holder.mView).scaleX(1.17f).scaleY(1.17f).translationZ(1).start();
        }
        holder.mTextName.setText(mListMusic.get(position).getName());

        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(mContext,"ViewClick" + position,Toast.LENGTH_SHORT).show();
                Log.e("TAG", "onClick: "+position+"~~~"+mListMusic.get(position).getPath());
                mIntent.putExtra("url",mListMusic.get(position).getPath());
                mIntent.putExtra("position",position);
                mContext.startService(mIntent);
            }
        });
        holder.mTextName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b){
                    ViewCompat.animate(holder.mView).scaleX(1.17f).scaleY(1.17f).translationZ(1).start();
                    mIntent.putExtra("url",mListMusic.get(position).getPath());
                    mIntent.putExtra("position",position);
                    mContext.startService(mIntent);
                }else{
                    ViewCompat.animate(holder.mView).scaleX(1f).scaleY(1f).translationZ(1).start();
                }
            }
        });
        holder.mView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                Log.e("TAG", "onFocusChange: "+b+"----"+position);
                    if (b){
                        ViewCompat.animate(holder.mView).scaleX(1.17f).scaleY(1.17f).translationZ(1).start();
                    }else{
                        ViewCompat.animate(holder.mView).scaleX(1f).scaleY(1f).translationZ(1).start();
                    }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mListMusic.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView mTextName;
        View mView;
        public ViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
            mTextName = (TextView) itemView.findViewById(R.id.item_text_name);
        }
    }
}
